JavaScript uses Unicode characters, not ASCII. As in C, characters may be equated with integers. For example, if ('a' > 64). Characters may be defined with octal or hex escapes. The less-than and greater-than symbols are represented respectively by \074 and \076 in octal and \x3c and \x3e in hex. (Upper- and lower-case are not considered. \x3c, \X3c and \X3C are equally valid.)
JavaScript provides five special characters for use in strings. Include these characters directly in your strings.
Searching strings | |
indexOf( aString , [aStart]) | Searches for a substring, optionally starting at a particular index, e.g., "Hello Dolly".indexOf("ll") returns 2. "Hello Dolly".indexOf("ll", 5) returns 8. If no substring is found, JavaScript returns -1. |
lastIndexOf( aString , [aStart]) | Searches backwards for a substring, optionally starting at a particular index, e.g., "Hello Dolly".indexOf("ll") returns 8. "Hello Dolly".indexOf("ll", 5) returns 2. If no substring is found, JavaScript returns -1. |
Converting strings | |
parseInt( aString) | Returns the integer value of a string. See the Math Overview for an interactive demonstration of parseInt(), e.g., parseInt("8") returns 8. parseInt("8.5") returns 8. parseInt("8Foo") returns 8. parseInt("Foo8") returns NaN (not a number). |
parseFloat( aString) | Returns the floating-point value of a string. See the Math Overview for an interactive demonstration of parseFloat() and a further description of legal floating-point numbers, e.g., parseFloat("8") returns 8. parseFloat("8.5") returns 8.5. parseFloat("8E2") returns 800. parseFloat("Foo8") returns NaN (not a number). |
"" + aNumber | Converts a number constant or variable to a string, e.g., ""+8 returns "8". ""+8.5 returns "8.5". ""+myVar returns the string value of the variable myVar. |
toLowerCase( aString) | Converts a string to lowercase, e.g., "Hello World".toLower() returns "hello world". |
toUpperCase( aString) | Converts a string to uppercase, e.g., "Hello World".toUpper() returns "HELLO WORLD". |
escape( aString) | Converts ISO Latin-1 characters to ASCII, e.g., escape("<hello world>") returns "%3CHello%20World%3E". |
unescape( aString) | Converts ASCII to ISO Latin-1, e.g., unescape("%3CHello%20World%3E") returns "<Hello World>". |
Other string operations | |
charAt( anIndex) | Returns the character found at an index. The index starts counting from zero, e.g., "Hello World".charAt(6) returns "W". |
substring( aStartIndex, anEndIndex) | Returns the characters found between the start and end indices, e.g., "Hello World".substring(0,1) returns "H". "Hello World".substring(4,10) returns "o Worl". |
string1 + string2 | Creates a new string which appends string1 with string2, e.g., "Hello "+"World" returns "Hello World" |
"" + aString | Creates a new string which "clones" the original string, e.g., ""+"Hello World" returns a fresh copy of "Hello World". |
length() | Returns the length of a string, e.g., "Hello World".length returns 11. |